﻿
using GalFrameWork.ViewModel;

namespace GalFrameWork.Utils;

public class OpenFileUtil {
    
    //获得一个Excel文件
    public async Task<FileResult> GetExcelFile() {
        var result = await FilePicker.Default.PickAsync(new PickOptions {
            FileTypes = new FilePickerFileType(
                new Dictionary<DevicePlatform, IEnumerable<string>> {
                    {DevicePlatform.iOS, new[] {"com.microsoft.excel.xlsx"}}, {
                        DevicePlatform.Android,
                        new[] {
                            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                        }
                    },
                    {DevicePlatform.WinUI, new[] {".xlsx", ".xls"}}
                }),
            PickerTitle = "选择Excel文件"
        });
        return result;
    }

    //获得一个jpg/png文件
    public async Task<FileResult> GetImageFile() {
        var result = await FilePicker.Default.PickAsync(new PickOptions {
            FileTypes = new FilePickerFileType(
                new Dictionary<DevicePlatform, IEnumerable<string>> {
                    {DevicePlatform.iOS, new[] {"public.image"}},
                    {DevicePlatform.Android, new[] {"image/*"}},
                    {DevicePlatform.WinUI, new[] {".jpg", ".png"}}
                }),
            PickerTitle = "选择图片文件"
        });
        return result;
    }

    public async Task<ImageSource> TestPickImage() {
        try {
            var result = await FilePicker.Default.PickAsync(PickOptions.Images);
            if (result != null) {
                using var stream = await result.OpenReadAsync();
                var image = ImageSource.FromStream(() => stream);
                return image;
            }

        } catch (Exception ex) {
            throw ex;
        }

        return null;


    }
}